package com.zhxw.hadoop;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by zhengxiongwu on 2017/2/10.
 */
public class RecommendedReducer extends Reducer<Text,Text,Text,Text> {

    public void reduce(Text key, Iterable<Text> values,
                       Context context
    ) throws IOException, InterruptedException {
        List<String> users = new ArrayList<String>();

        for (Text val : values) {
            context.write(new Text("----每次的值----"),val);
            users.add(val.toString());
        }

        //context.write(new Text("----data----"),new Text(users.toString()));
        //context.write(new Text("----size----"),new Text("" + users.size()));
        if(users.size() > 1){
            //context.write(new Text("----进入if----"),new Text("----game begin----"));
            for(int i = 0; i < users.size(); i++){
                //context.write(new Text("----进入二层for----"),new Text("------------"));
                StringBuffer friends = new StringBuffer();
                friends.append("[");
                for(int j = 0; j < users.size(); j++){
                    if(i != j){
                        friends.append(users.get(j)).append(" ");
                    }
                }
                friends.append("]");
                context.write(new Text(users.get(i)+"的推荐好友："),new Text(friends.toString()));
                //context.write(new Text("----end----"),new Text("----game over----"));
            }
        }

    }
}
